package com.klhappy.houyuan.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.klhappy.houyuan.R;
import com.klhappy.houyuan.constant.KeyConstant;
import com.klhappy.houyuan.view.AbsActivity;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2015/11/13.
 */
public class PayTypesActivity extends AbsActivity {

    @InjectView(R.id.title)
    TextView title;
    @InjectView(R.id.cb_del_1)
    CheckBox cb_del_1;
    @InjectView(R.id.cb_del_2)
    CheckBox cb_del_2;
    @InjectView(R.id.cb_del_3)
    CheckBox cb_del_3;

    @OnClick(R.id.click_ra)
    public void click_ra() {
        intent.putExtra("pay", pay);
        setResult(10, intent);
        finish();
    }

    private String pay;
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_types);
        ButterKnife.inject(this);
        title.setText("付款方式");
        intent = new Intent();
        mConfigManage.remove(KeyConstant.PAY);
        pay = "支付宝";
        mConfigManage.set(KeyConstant.PAY, pay);
        setListener();
    }

    private void setListener() {
        cb_del_1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    mConfigManage.remove(KeyConstant.PAY);
                    pay = "支付宝";
                    checked();
                    cb_del_1.setChecked(true);
                    mConfigManage.set(KeyConstant.PAY, pay);
                }
            }
        });
        cb_del_2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    mConfigManage.remove(KeyConstant.PAY);
                    pay = "微信";
                    checked();
                    cb_del_2.setChecked(true);
                    mConfigManage.set(KeyConstant.PAY, pay);
                }
            }
        });
        cb_del_3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    mConfigManage.remove(KeyConstant.PAY);
                    pay = "货到付款";
                    checked();
                    cb_del_3.setChecked(true);
                    mConfigManage.set(KeyConstant.PAY, pay);
                }
            }
        });
    }

    private void checked() {
        cb_del_1.setChecked(false);
        cb_del_2.setChecked(false);
        cb_del_3.setChecked(false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {
            intent.putExtra("pay", pay);
            setResult(10, intent);
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
